package com.huawei.hms.framework.network.restclient;

import android.content.Context;
import com.huawei.hms.framework.common.CheckParamUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.Converter.Factory;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpClient;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpClientGlobalInstance;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import com.huawei.hms.framework.network.restclient.hwhttp.ResponseBody;
import com.huawei.hms.framework.network.restclient.hwhttp.url.HttpUrl;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;

public class RestClient {
    private static final String TAG = "RestClient";
    private final HttpUrl baseUrl;
    @Nullable
    private final Executor callbackExecutor;
    private final List<Factory> converterFactories;
    private final HttpClient httpClient;
    private final Map<Method, SubmitMethod<?, ?>> methodMap = new ConcurrentHashMap();
    private final List<SubmitAdapter.Factory> submitAdapterFactories;
    private final boolean validateEagerly;

    public static final class Builder {
        private HttpUrl baseUrl;
        @Nullable
        private Executor callbackExecutor;
        private final List<Factory> converterFactories = new ArrayList();
        HttpClient httpClient;
        private final List<SubmitAdapter.Factory> submitAdapterFactories = new ArrayList();
        private boolean validateEagerly;

        public Builder(Context context) {
            HttpClientGlobalInstance.getInstance().init(context);
        }

        Builder(RestClient restClient) {
            this.baseUrl = restClient.baseUrl;
            this.converterFactories.addAll(restClient.converterFactories);
            this.converterFactories.remove(0);
            this.submitAdapterFactories.addAll(restClient.submitAdapterFactories);
            this.submitAdapterFactories.remove(this.submitAdapterFactories.size() - 1);
            this.callbackExecutor = restClient.callbackExecutor;
            this.validateEagerly = restClient.validateEagerly;
            this.httpClient = restClient.httpClient;
        }

        public Builder baseUrl(HttpUrl httpUrl) {
            CheckParamUtils.checkNotNull(this.baseUrl, "baseUrl == null");
            this.baseUrl = httpUrl;
            return this;
        }

        public Builder baseUrl(String str) {
            this.baseUrl = new HttpUrl(str);
            return this;
        }

        public Builder httpClient(HttpClient httpClient) {
            this.httpClient = httpClient;
            return this;
        }

        public Builder addConverterFactory(Factory factory) {
            this.converterFactories.add(CheckParamUtils.checkNotNull(factory, "factory == null"));
            return this;
        }

        public Builder addSubmitAdapterFactory(SubmitAdapter.Factory factory) {
            this.submitAdapterFactories.add(CheckParamUtils.checkNotNull(factory, "factory == null"));
            return this;
        }

        public Builder callbackExecutor(Executor executor) {
            this.callbackExecutor = (Executor) CheckParamUtils.checkNotNull(executor, "executor == null");
            return this;
        }

        public List<SubmitAdapter.Factory> submitAdapterFactories() {
            return this.submitAdapterFactories;
        }

        public List<Factory> converterFactories() {
            return this.converterFactories;
        }

        public Builder validateEagerly(boolean z) {
            this.validateEagerly = z;
            return this;
        }

        public RestClient build() {
            if (this.baseUrl == null) {
                Logger.w(RestClient.TAG, "may be you need a baseUrl");
            }
            Executor executor = this.callbackExecutor;
            List arrayList = new ArrayList(this.submitAdapterFactories);
            arrayList.add(defaultSubmitAdapterFactory(executor));
            List arrayList2 = new ArrayList(this.converterFactories.size() + 1);
            arrayList2.add(new BuiltInConverters());
            arrayList2.addAll(this.converterFactories);
            Logger.d(RestClient.TAG, "build time = " + Version.getBuildTime());
            return new RestClient(this.baseUrl, Collections.unmodifiableList(arrayList2), Collections.unmodifiableList(arrayList), executor, this.validateEagerly, this.httpClient);
        }

        private SubmitAdapter.Factory defaultSubmitAdapterFactory(@Nullable Executor executor) {
            if (executor != null) {
                return new ExecutorSubmitAdapterFactory(executor);
            }
            return DefaultSubmitAdapterFactory.INSTANCE;
        }
    }

    RestClient(HttpUrl httpUrl, List<Factory> list, List<SubmitAdapter.Factory> list2, @Nullable Executor executor, boolean z, HttpClient httpClient) {
        this.baseUrl = httpUrl;
        this.converterFactories = list;
        this.submitAdapterFactories = list2;
        this.callbackExecutor = executor;
        this.validateEagerly = z;
        if (httpClient != null) {
            this.httpClient = httpClient;
        } else {
            this.httpClient = new com.huawei.hms.framework.network.restclient.hwhttp.HttpClient.Builder().build();
        }
    }

    public <T> T create(final Class<T> cls) {
        if (!cls.isInterface()) {
            throw new IllegalArgumentException("API declarations must be interfaces.");
        } else if (cls.getInterfaces().length > 0) {
            throw new IllegalArgumentException("API interfaces must not extend other interfaces.");
        } else {
            if (this.validateEagerly) {
                eagerlyloadMethods(cls);
            }
            return Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, new InvocationHandler() {
                public Object invoke(Object obj, Method method, @Nullable Object[] objArr) {
                    SubmitMethod access$000 = RestClient.this.loadServiceMethod(method, cls);
                    return access$000.adapt(new RealSubmit(RestClient.this.httpClient, access$000, objArr));
                }
            });
        }
    }

    private void eagerlyloadMethods(Class<?> cls) {
        for (Method loadServiceMethod : cls.getDeclaredMethods()) {
            loadServiceMethod(loadServiceMethod, cls);
        }
    }

    private SubmitMethod<?, ?> loadServiceMethod(Method method, Class cls) {
        SubmitMethod<?, ?> submitMethod = (SubmitMethod) this.methodMap.get(method);
        if (submitMethod == null) {
            synchronized (this.methodMap) {
                submitMethod = (SubmitMethod) this.methodMap.get(method);
                if (submitMethod == null) {
                    submitMethod = new Builder(this, method, cls).build();
                    this.methodMap.put(method, submitMethod);
                }
            }
        }
        return submitMethod;
    }

    public HttpUrl getBaseUrl() {
        return this.baseUrl;
    }

    public List<SubmitAdapter.Factory> getSubmitAdapterFactories() {
        return this.submitAdapterFactories;
    }

    public SubmitAdapter<?, ?> submitAdapter(Type type, Annotation[] annotationArr) {
        return nextSubmitAdapter(null, type, annotationArr);
    }

    public SubmitAdapter<?, ?> nextSubmitAdapter(SubmitAdapter.Factory factory, Type type, Annotation[] annotationArr) {
        CheckParamUtils.checkNotNull(type, "returnType == null");
        CheckParamUtils.checkNotNull(annotationArr, "annotations == null");
        int indexOf = this.submitAdapterFactories.indexOf(factory) + 1;
        int size = this.submitAdapterFactories.size();
        for (int i = indexOf; i < size; i++) {
            SubmitAdapter<?, ?> submitAdapter = ((SubmitAdapter.Factory) this.submitAdapterFactories.get(i)).get(type, annotationArr, this);
            if (submitAdapter != null) {
                return submitAdapter;
            }
        }
        throw new IllegalArgumentException("Could not locate submit adapter for " + type + ".\n");
    }

    public List<Factory> getConverterFactories() {
        return this.converterFactories;
    }

    public <T> Converter<T, RequestBody> requestBodyConverter(Type type, Annotation[] annotationArr, Annotation[] annotationArr2) {
        return nextRequestBodyConverter(null, type, annotationArr, annotationArr2);
    }

    public <T> Converter<T, RequestBody> nextRequestBodyConverter(@Nullable Factory factory, Type type, Annotation[] annotationArr, Annotation[] annotationArr2) {
        CheckParamUtils.checkNotNull(type, "type == null");
        CheckParamUtils.checkNotNull(annotationArr, "parameterAnnotations == null");
        CheckParamUtils.checkNotNull(annotationArr2, "methodAnnotations == null");
        int indexOf = this.converterFactories.indexOf(factory) + 1;
        int size = this.converterFactories.size();
        for (int i = indexOf; i < size; i++) {
            Converter<T, RequestBody> requestBodyConverter = ((Factory) this.converterFactories.get(i)).requestBodyConverter(type, annotationArr, annotationArr2, this);
            if (requestBodyConverter != null) {
                return requestBodyConverter;
            }
        }
        throw new IllegalArgumentException("Could not locate RequestBody converter for " + type + ".\n");
    }

    public <T> Converter<ResponseBody, T> responseBodyConverter(Type type, Annotation[] annotationArr) {
        return nextResponseBodyConverter(null, type, annotationArr);
    }

    public <T> Converter<ResponseBody, T> nextResponseBodyConverter(@Nullable Factory factory, Type type, Annotation[] annotationArr) {
        CheckParamUtils.checkNotNull(type, "type == null");
        CheckParamUtils.checkNotNull(annotationArr, "annotations == null");
        int indexOf = this.converterFactories.indexOf(factory) + 1;
        int size = this.converterFactories.size();
        for (int i = indexOf; i < size; i++) {
            Converter<ResponseBody, T> responseBodyConverter = ((Factory) this.converterFactories.get(i)).responseBodyConverter(type, annotationArr, this);
            if (responseBodyConverter != null) {
                return responseBodyConverter;
            }
        }
        throw new IllegalArgumentException("Could not locate ResponseBody converter for " + type + ".\n");
    }

    public <T> Converter<T, String> stringConverter(Type type, Annotation[] annotationArr) {
        CheckParamUtils.checkNotNull(type, "type == null");
        CheckParamUtils.checkNotNull(annotationArr, "annotations == null");
        int size = this.converterFactories.size();
        for (int i = 0; i < size; i++) {
            Converter<T, String> stringConverter = ((Factory) this.converterFactories.get(i)).stringConverter(type, annotationArr, this);
            if (stringConverter != null) {
                return stringConverter;
            }
        }
        return ToStringConverter.INSTANCE;
    }

    @Nullable
    public Executor getCallbackExecutor() {
        return this.callbackExecutor;
    }

    public Builder newBuilder() {
        return new Builder(this);
    }

    public HttpClient getHttpClient() {
        return this.httpClient;
    }
}
